package com.imooc.sellspringcloud.server.service.impl;


import com.imooc.product.common.DecreaseStockInput;
import com.imooc.sellspringcloud.server.dataobject.ProductInfo;
import com.imooc.sellspringcloud.server.enumUtils.ProductExceptionEnum;
import com.imooc.sellspringcloud.server.enumUtils.ProductStatusEnum;
import com.imooc.sellspringcloud.server.exception.ProductException;
import com.imooc.sellspringcloud.server.repository.ProductInfoRepository;
import com.imooc.sellspringcloud.server.service.ProductInfoService;
import com.imooc.sellspringcloud.server.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author
 * @create 2019-07-27 17:45
 **/
@Service
@Slf4j
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository productInfoRepository;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public List<ProductInfo> findUpAll() {
        List<ProductInfo> productInfos = productInfoRepository
                .findByProductStatus(ProductStatusEnum.UP.getStatus());
        return productInfos;
    }

    @Override
    public List<ProductInfo> findProductsByIds(List<String> ids) {
        return productInfoRepository.findAllById(ids);
    }

    /**
     * 减少库存
     * @param decreaseStockInputs
     * @return
     */
    @Override

    public boolean reduceProductByIds(List<DecreaseStockInput> decreaseStockInputs) {
        reduceProduct(decreaseStockInputs);
        String json = JsonUtil.toJson(decreaseStockInputs);
        rabbitTemplate.convertAndSend("product-reduce",
                "productReduceKey",json);
        return true;
    }

    @Transactional
    public void reduceProduct(List<DecreaseStockInput> decreaseStockInputs) {
        // 先查询库存
        for (DecreaseStockInput decreaseStockInput : decreaseStockInputs) {
            Optional<ProductInfo> optional = productInfoRepository.findById(decreaseStockInput.getProductId());
            ProductInfo productInfo = optional.get();
            int number = productInfo.getProductStock() - decreaseStockInput.getProductQuantity();
            // 库存不足抛出异常
            if(number < 0) {
                throw new ProductException(ProductExceptionEnum.PRODUCT_NOT_ENOUGH.getCode(),
                        ProductExceptionEnum.PRODUCT_NOT_ENOUGH.getMessage());
            }
            // 入库保存
            productInfo.setProductStock(number);
            try {
                productInfoRepository.save(productInfo);
            } catch (Exception e) {
                log.error("商品入库失败 {}", e.getMessage());
                throw new ProductException(ProductExceptionEnum.PRODUCT_WAREHOUSING_FAIL.getCode(),
                        e.getMessage());
            }
        }
    }
}